package nostalgia.playlist;

import nostalgia.collection.DirectoryCollectionItem;

import java.util.ArrayList;
import java.util.Iterator;
import java.io.*;

public class M3UPlaylist extends ArrayList<DirectoryCollectionItem> {

    public void save(File f) throws IOException {
        save(new FileOutputStream(f));
    }

    public void save(OutputStream out) throws IOException {
        Writer writer = new OutputStreamWriter(out, "UTF8");
        writer.write("#EXTM3U\n\n");
        Iterator<DirectoryCollectionItem> it = iterator();
        while(it.hasNext()) {
            DirectoryCollectionItem item = it.next();
            writer.write("#EXTINF:");
            writer.write(Long.toString(item.getDuration()));
            writer.write(",");
            writer.write(item.getTitle());
            writer.write("\n");
            writer.write(item.getFile());
            writer.write("\n\n");
        }
        writer.close();
    }
}
